package com.bs.visitservice.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bs.visitservice.entity.Seller;
import com.bs.visitservice.entity.UserInfo;
import com.bs.visitservice.service.UserInfoService;
import com.bs.visitservice.utils.ParamUtils;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class UserManagerController {

    @Autowired
    private UserInfoService userInfoService;
    @RequestMapping(value = "queryUserInfo",method = RequestMethod.POST)
    public Result queryUserInfo(@RequestBody Map cond){
        UserInfo userInfo = (UserInfo) ResultUtil.transObject(cond, UserInfo.class);
        IPage page = ParamUtils.getPage(cond);
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<UserInfo>();
        if(userInfo.getNo() != null){
            wrapper.eq(UserInfo::getNo,userInfo.getNo());
        }
        if(StringUtils.isNotEmpty(userInfo.getNickname())) {
            wrapper.like(UserInfo::getNickname, userInfo.getNickname());
        }
        page = userInfoService.pageMaps(page, wrapper);
        List<Map<String,Object>> records = page.getRecords();
        ResultUtil.transfort(records,getDictMap(),true);
        Map<String,Object> result = new HashMap<>();
        result.put("total",page.getTotal());
        result.put("datas",records);
        return Result.success(result);
    }

    @RequestMapping(value = "blockUser",method = RequestMethod.POST)
    public Result blockUser(@RequestBody Map cond){
        UserInfo userInfo = (UserInfo) ResultUtil.transObject(cond, UserInfo.class);
        userInfo.setStatus("1");
        boolean isSuc = userInfoService.updateById(userInfo);
        if(isSuc){
            return Result.success(null);
        }
        return Result.error(null);
    }

    /**
     * 解封
     * @param cond
     * @return
     */
    @RequestMapping(value = "unBlockUser",method = RequestMethod.POST)
    public Result unBlockUser(@RequestBody Map cond){
        UserInfo userInfo = (UserInfo) ResultUtil.transObject(cond, UserInfo.class);
        userInfo.setStatus("0");
        boolean isSuc = userInfoService.updateById(userInfo);
        if(isSuc){
            return Result.success(null);
        }
        return Result.error(null);
    }
    /**
     * 获取列和字典值的映射关系
     * @return
     */
    private Map getDictMap(){
        Map<String,String> dictMap = new HashMap<>();
        dictMap.put("status","accountStatus");
        return dictMap;
    }

}
